import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;

import java.util.ArrayList;

public class SentinelMain {

    public static void main(String[] args) {

        initFlowRules();

        while (true){

            try (
                    Entry entry= SphU.entry("HelloWorld")

                    ){

                //业务开始
                System.out.println("hello world");
                //业务结束

            }catch (BlockException e){
                //流控逻辑处理开始
                System.out.println("block");
                //流控逻辑处理结束
            }
        }


    }

    private static void initFlowRules(){
        ArrayList<FlowRule> rules = new ArrayList<>();
        FlowRule rule = new FlowRule();
        rule.setResource("HelloWorld");
        rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        // Set limit QPS to 20.
        rule.setCount(20);
        rules.add(rule);

        FlowRuleManager.loadRules(rules);

    }
}
